package com.atwisdom.star.core.bsl.aop;

import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.vo.ActionData;
import com.atwisdom.star.core.bsl.erp.ErpServiceClient;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Aspect
public class ErpMaterialCategoryAspect {

    @Autowired
    private ErpServiceClient erpServiceClient;

    @Pointcut("@annotation(com.atwisdom.star.core.bsl.aop.ErpMaterialCategoryAnnotation)")
    private void pointcut(){
    }
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        log.info("before===");
        String methodName = pjp.getSignature().getName();
        Object[] args = pjp.getArgs();
        Object arg = args[0];
        if ("saveHierWithoutModel".equals(methodName)) {
            JSONObject dataJson = ((ActionData<JSONObject>) arg).getData().get(0);
            if (Long.valueOf("256").equals(dataJson.getLong("containerId"))) {
                JSONObject values = dataJson.getJSONObject("values");
                String code = values.getString("value");
                String name = values.getString("label");
                Long id = dataJson.getLong("id");
                // 调用erp
                erpServiceClient.bdinvclSave(dataJson);
                if (id == null) {
                    // 新增物料类型
                    values.put("pk_corp", "11111");
                } else {
                    // 更新物料类型
                    values.put("pk_corp", "11111");
                }
            }
        } else if ("deleteHierByIdList".equals(methodName)) {

        }


        //将新的参数传入
        Object ret = pjp.proceed(args);
        log.info("返回的结果为:"+JSONObject.toJSONString(ret));
        log.info("after===");
        return ret;
    }
}
